<?php

require_once 'Base/ActivityPeer.php';

/**
 * Generated Model class.
 */
class ActivityPeer extends Base_ActivityPeer
{
	protected $_actions_list = array(
		'add' => array('добавила', 'добавил'),
		'del' => array('удалила', 'удалил'),
		'cron' => array('добавлено', 'пропущено', 'обновленно')
	);
	protected $_messages = array(
		'reg' => '%s, новый пользователь',
		'posting' => '%s %s <a href="/posting/get?id=%s">пост</a>',
		'delete' => '%s %s <a href="/posting/get?id=%s">пост</a>',
		'cron' => 'Парсер <b><u>%s</u></b><p>Обработал %d %s из них:</p><p>Добавил: %d %s</p><p>Обновил: %d %s</p><p><u>%s</u></p>'
	);
	
        /**
         *
         * @param VkUser $user
         * @param int $post_id 
         */
	public function posting(VkUser $user, $post_id)
        {
            $action = ($user->getSex() == 1) ? $this->_actions_list['add'][0] : $this->_actions_list['add'][1]; 
            $message = sprintf($this->_messages['posting'], $user->getName() . ' ' . $user->getSurname(), $action, $post_id);

            $row = $this->createRow();
            $row->setUserId($user->getId());
            $row->setText($message);
            $row->setCreatedAt(date('Y-m-d H:i:s'));
            $row->save();
	}
        
        /**
         *
         * @param VkUser $user_id
         * @param int $post_id 
         */        
        public function del(VkUser $user, $post_id)
        {
            $action = ($user->getSex() == 1) ? $this->_actions_list['del'][0] : $this->_actions_list['del'][1]; 
            $message = sprintf($this->_messages['delete'], $user->getName() . ' ' . $user->getSurname(), $action, $post_id);
            
            $row = $this->createRow();
            $row->setUserId($user->getId());
            $row->setText($message);
            $row->setCreatedAt(date('Y-m-d H:i:s'));
            $row->save();
	}
	
	public function crone_work($crone_name, $parse_url, $all, $add, $update){
		$_post = $this->number_ending($all);
		$_add = $this->number_ending($add);
		$_upd = $this->number_ending($update);
		
		$message = sprintf($this->_messages['cron'], $crone_name, $all, $_post, $add, $_add, $update, $_upd, $parse_url);
		
		$this->insert(array(
			'id' => '',
			'user_id' => 0,
			'text' => $message,
			'created_at' => date('Y-m-d H:i:s')
		));
	}
	
	public function number_ending($number, $ending0 = 'записей', $ending1 = 'запись', $ending2 = 'записи') {
		$num100 = $number % 100;
		$num10 = $number % 10;
		if ($num100 >= 5 && $num100 <= 20) {
			return $ending0;
		} else if ($num10 == 0) {
			return $ending0;
		} else if ($num10 == 1) {
			return $ending1;
		} else if ($num10 >= 2 && $num10 <= 4) {
			return $ending2;
		} else if ($num10 >= 5 && $num10 <= 9) {
			return $ending0;
		} else {
			return $ending2;
		}
	}
	
	public function addUser(VkUser $vkuser)
        {
            $message = sprintf($this->_messages['reg'], $vkuser->getName());
            $row = $this->createRow();
            $row->setUserId($vkuser->getId());
            $row->setText($message);
            $row->setCreatedAt(date('Y-m-d H:i:s'));
            $row->save();
	}
	
	public function getList($limit = 10)
        {
            $select = $this->select()->order('created_at DESC')->limit($limit);
            $result = $this->fetchAll($select);
            return $result->toArray();
	}
}

